The if-else Construct

I've included BASIC examples, so that programmers who are familiar with BASIC or FORTRAN will feel more at ease with the C/C++/Java style of syntax that D employs. They are definitely different, but one is not necessarily better than the other. I've always found that I'm more comfortable with things that are familiar to me. Also, I have a heavy BASIC background (AppleBASIC, GW-BASIC, QuickBASIC, RapidQ, BCX, B++), so D syntax doens't come as naturally to me as BASIC syntax.

The Digital Mars D compilers makes it easy to check these samples as the HTML file itself can be compiled directly (e.g., dmd if-else.html). I make certain that these examples compile with D before I spend too much time on them.

QuickBASIC
DIM I%
IF I% = 1 THEN PRINT "It's one!" ELSE PRINT "It's not one!"
D
void main()
{
    int i;
    if(i==0) printf("It's one!"); else printf("It's not one!");
}
Output
It's not one!

Both QuickBASIC and D automatically initialize integers to 0. (That's why the integer in my example isn't equal to 1.)


Return to Tutorial Overview